package com.masterclient.app.modules.shop.contract;

import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselibrary.bean.shop.DealerShippingListBean;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.baselib.container.CommonBaseResponse;
import com.masterclient.app.httpservice.RetrofitUtil2;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

/**
 * @auther hjian
 * @date 2020/9/12.
 */
public class DealerShippingListPresenter extends DealerShippingListContract.Presenter{

    @Override
    public void getShippingList(String deliveryId) {
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().dealerShippingList(deliveryId)
                , new Consumer<CommonBaseResponse<DealerShippingListBean>>() {
                    @Override
                    public void accept(CommonBaseResponse<DealerShippingListBean> listBean) throws Exception {
                        if (listBean.getCode() == 0){
                            mView.showData(listBean.getData().getList());
                        }else {
                            mView.showFailure(listBean.getMsg());
                        }
                    }
                }, new ThrowableConsumer(){
                    @Override
                    public void accept(Throwable o) throws Exception {
                        super.accept(o);
                        mView.showFailure("网络异常，请重试");
                    }
                }));
    }

    @Override
    public void setDefault(String id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("delivery_id",id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().dealerShippingDefault(body), new Consumer<CommonBaseResponse>() {
            @Override
            public void accept(@NonNull CommonBaseResponse bean) throws Exception {
                if(bean.getCode() == 0){
                    mView.defaultResult(true);
                }else {
                    mView.defaultResult(false);
                    ToastUtil.showToast(bean.getMsg());
                }
            }
        }, new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.defaultResult(false);
                ToastUtil.showToast("网络异常，请重试");
            }
        }));
    }

    @Override
    public void selectShipping(String product_id, String delivery_id) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("product_id",product_id);
            jsonObject.put("delivery_id",delivery_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        mRxManager.add(toSubscribe(RetrofitUtil2.getInstance().dealerShippingSelected(body), new Consumer<CommonBaseResponse>() {
            @Override
            public void accept(@NonNull CommonBaseResponse bean) throws Exception {
                if(bean.getCode() == 0){
                    mView.showSelectResult(true);
                }else {
                    mView.showSelectResult(false);
                    ToastUtil.showToast(bean.getMsg());
                }
            }
        }, new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.showSelectResult(false);
                ToastUtil.showToast("网络异常，请重试");
            }
        }));
    }
}
